package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Filler;
import cn.edu.scau.cmi.ema.domain.Foodbatch;
import cn.edu.scau.cmi.ema.domain.Foodbatchfiller;
import cn.edu.scau.cmi.ema.domain.Unit;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("FoodbatchfillerController")
public class FoodbatchfillerController extends CmiEmaController {

	@RequestMapping("/listFoodbatchfillers")
	public ModelAndView listFoodbatchfillers(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = foodbatchfillerPaginationService.updatePagination(request, pagination);
		Set<Foodbatchfiller> foodbatchfillers = foodbatchfillerPaginationService
				.getPaginationFoodbatchfillers(pagination);

		mav.addObject("foodbatchfillers", foodbatchfillers);
		mav.addObject("entityName", "foodbatchfiller");
		mav.addObject("pagination", pagination);
		mav.setViewName("foodbatchfiller/listFoodbatchfillers");
		return mav;
	}

	public String listFoodbatchfillers() {
		return "redirect:/listFoodbatchfillers";
	}

	@RequestMapping("/viewFoodbatchfiller")
	public ModelAndView viewFoodbatchfiller(@RequestParam Integer foodbatchfillerId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("foodbatchfiller", foodbatchfillerDAO.findFoodbatchfillerByPrimaryKey(foodbatchfillerId));
		mav.setViewName("foodbatchfiller/viewFoodbatchfiller");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFoodbatchfillers")
	public ModelAndView confirmDeleteFoodbatchfillers(
			@RequestParam(value = "deleteFoodbatchfillersIds", required = true) Set<Integer> deleteFoodbatchfillersIds) {
		ModelAndView mav = new ModelAndView();

		Set<Foodbatchfiller> deleteFoodbatchfillers = foodbatchfillerDAO
				.findFoodbatchfillersByIds(deleteFoodbatchfillersIds);
		Set<Foodbatchfiller> referencedFoodbatchfillers = foodbatchfillerService
				.loadReferenceFoodbatchfillers(deleteFoodbatchfillers);
		boolean hasReferenced;
		if (referencedFoodbatchfillers.size() == 0) {
			mav.addObject("deleteEntitys", deleteFoodbatchfillers);
			hasReferenced = false;
		} else {
			deleteFoodbatchfillers.removeAll(referencedFoodbatchfillers);
			mav.addObject("referencedEntitys", referencedFoodbatchfillers);
			mav.addObject("deleteEntitys", deleteFoodbatchfillers);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Foodbatchfiller");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFoodbatchfillers");
		return mav;
	}

	@RequestMapping("/deleteFoodbatchfiller")
	public String deleteFoodbatchfiller(@RequestParam Integer foodbatchfillerId) {
		Foodbatchfiller foodbatchfiller = foodbatchfillerDAO.findFoodbatchfillerByPrimaryKey(foodbatchfillerId);
		foodbatchfillerService.deleteFoodbatchfiller(foodbatchfiller);
		return "forward:/listFoodbatchfillers";
	}

	@RequestMapping("/confirmDeleteFoodbatchfiller")
	public ModelAndView confirmDeleteFoodbatchfiller(@RequestParam Integer foodbatchfillerId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("foodbatchfiller", foodbatchfillerDAO.findFoodbatchfillerByPrimaryKey(foodbatchfillerId));

		mav.setViewName("foodbatchfiller/deleteFoodbatchfiller");

		return mav;
	}

	@RequestMapping("/deleteFoodbatchfillers")
	public String deleteFoodbatchfillers(
			@RequestParam(value = "deleteFoodbatchfillersId", required = true) Set<Integer> deleteFoodbatchfillersId) {
		foodbatchfillerService.deleteFoodbatchfillers(deleteFoodbatchfillersId);
		return "forward:/listFoodbatchfillers";
	}

	@RequestMapping("/editFoodbatchfiller")
	public ModelAndView editFoodbatchfiller(@RequestParam Integer foodbatchfillerId) {

		ModelAndView mav = new ModelAndView();

		Foodbatchfiller foodbatchfiller = foodbatchfillerDAO.findFoodbatchfillerByPrimaryKey(foodbatchfillerId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("foodbatchfiller", foodbatchfiller);

		// 把所有的引用foodbatchfiller对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodbatchfillerService.loadFoodbatchfillerObjectReferencedSet(foodbatchfiller));

		// 将编辑页面foodbatchfiller添加到mav中
		mav.setViewName("foodbatchfiller/editFoodbatchfiller");
		return mav;
	}

	@RequestMapping("/newFoodbatchfiller")
	public ModelAndView newFoodbatchfiller() {
		ModelAndView mav = new ModelAndView();
		Foodbatchfiller foodbatchfiller = new Foodbatchfiller();

		mav.addObject("foodbatchfiller", foodbatchfiller);

		// 把所有的引用foodbatchfiller对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodbatchfillerService.loadFoodbatchfillerObjectReferencedSet(foodbatchfiller));

		mav.addObject("newFlag", true);
		mav.setViewName("foodbatchfiller/editFoodbatchfiller");

		return mav;
	}

	@RequestMapping("/saveFoodbatchfiller")
	public ModelAndView saveFoodbatchfiller(@ModelAttribute Foodbatchfiller foodbatchfiller) {

		ModelAndView mav = new ModelAndView();

		Filler fillerTemp = foodbatchfiller.getFiller();
		Filler filler = fillerTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(fillerDAO.findFillerById(fillerTemp.getId()));
		Foodbatch foodbatchTemp = foodbatchfiller.getFoodbatch();
		Foodbatch foodbatch = foodbatchTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(foodbatchDAO.findFoodbatchById(foodbatchTemp.getId()));
		Unit unitTemp = foodbatchfiller.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Foodbatchfiller existingFoodbatchfiller = CmiHibernateProxyUtil
				.deproxy(foodbatchfillerDAO.findFoodbatchfillerByPhysicalPrimaryKey(foodbatch.getId(), filler.getId()));

		foodbatchfiller.setFiller(filler);
		foodbatchfiller.setFoodbatch(foodbatch);
		foodbatchfiller.setUnit(unit);
		// 新建记录
		if (foodbatchfiller.getId() == null) {
			if (foodbatchfiller.equals(existingFoodbatchfiller)) {

				mav.addAllObjects(foodbatchfillerService.loadFoodbatchfillerClassReferencedSet(foodbatchfiller));
				// mav = editContinue(mav, foodbatchfiller);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodbatchfiller/editFoodbatchfiller");
			} else {
				foodbatchfillerService.saveFoodbatchfiller(foodbatchfiller);
				mav.setViewName("forward:/listFoodbatchfillers");
			}
		}
		// 编辑记录
		else {
			Foodbatchfiller originFoodbatchfiller = CmiHibernateProxyUtil
					.deproxy(foodbatchfillerDAO.findFoodbatchfillerByPrimaryKey(foodbatchfiller.getId()));
			if (!foodbatchfiller.equals(originFoodbatchfiller) && foodbatchfiller.equals(existingFoodbatchfiller)) {
				mav.addAllObjects(foodbatchfillerService.loadFoodbatchfillerClassReferencedSet(foodbatchfiller));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodbatchfiller/editFoodbatchfiller");
			} else {
				foodbatchfillerService.saveFoodbatchfiller(foodbatchfiller);
				mav.setViewName("forward:/listFoodbatchfillers");
			}
		}
		return mav;
	}

	@RequestMapping("/foodbatchfillerController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
